layui.use(['layer', 'element', 'laytpl', 'jquery', 'table', 'upload', 'form'], function() {
	//引入模块
	var layer = layui.layer,
		element = layui.element,
		laytpl = layui.laytpl,
		form = layui.form,
		upload = layui.upload,
		table = layui.table,
		$ = layui.jquery;

	if (localStorage.set != null) {
		//加载页面发生请求拿到数据
		$.ajax({
			url: "http://localhost:8080/sc/admin?type=home",
			async: true, //异步
			type: "POST",
			headers: {
				"token": localStorage.token
			},
			success: function(data) {
				//这里找id必须用js
				if (data.code == 'TokenFailed') {
					//删除token set
					delete localStorage.token;
					delete localStorage.set;
					layer.msg('登录失效，请重新登录！')
					setTimeout(function() {
						//调用父页面方法
						parent.tz();
					}, 1000);
				} else {
					var getTpl = document.getElementById('homemb').innerHTML,
						view = document.getElementById('homemoban');
					laytpl(getTpl).render(data, function(html) {
						view.innerHTML = html;
					});
				}
			}
		});

	} else {
		layer.msg('您还没有登录！')
		setTimeout(function() {
			parent.tz();
			//window.location.replace('admin_login.html');
		}, 1000);
	}

	//自动重载
	var token = localStorage.token;
	//表格重载
	table.reload('testReload', {
		url: 'http://localhost:8080/sc/admin3?type=getOrderNo',
		headers: {
			token: token
		} //设定异步数据接口的额外参数
	});

	//发货点击事件
	$('#fahuo').on('click', function() {
		var ddbh = $('#window_id').text();
		//发送订单编号
		$.ajax({
			url: "http://localhost:8080/sc/admin3?type=fahuo&ddbh=" + ddbh,
			async: true, //异步
			type: "POST",
			headers: {
				"token": localStorage.token
			},
			success: function(data) {
				layer.msg(data.msg)
				//表格重载
				table.reload('testReload', {
					url: 'http://localhost:8080/sc/admin3?type=getOrderNo',
					headers: {
						token: token
					} //设定异步数据接口的额外参数
				});
			}
		});
		layer.closeAll();
	})

	//搜索点击事件
	$('#ss_button').on('click', function() {
		//拿到输入框的值
		var keyword = $('#ss_input').val();
		//表格重载
		table.reload('testReload', {
			url: 'http://localhost:8080/sc/admin3?type=queryOrderNo',
			where: {
				keyword: keyword
			} //设定异步数据接口的额外参数
		});
	});

	//监听行工具栏
	table.on('tool(tooltable)', function(obj) {
		var datatable = obj.data; //获得当前行数据
		var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
		var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
		if (layEvent == 'look') { //查看按钮点击事件
			//拿到订单号
			var orderbh = datatable.orderbh;
			$('#window_id').text(datatable.orderbh);
			$.ajax({
				url: "http://localhost:8080/sc/admin3?type=lookOrdercl&ddbh=" + orderbh,
				async: true, //异步
				type: "POST",
				headers: {
					"token": localStorage.token
				},
				success: function(data) {
					for (let key in data.list) {
						//拿到数据赋值给用户部分
						var name = data.list[key].name; //姓名
						var portrait = data.list[key].portrait; //头像
						var username = data.list[key].username; //账号
						var authority = data.list[key].authority; //用户状态
						var address = data.list[key].address; //地址
						var tel = data.list[key].tel; //电话
						var email = data.list[key].email; //邮箱
					}
					//赋值
					$('#window_name').text(name);
					//$('#window_name').text(portrait);
					$('#window_username').text(username);
					if (authority == '1') {
						$('#window_state').text("正常状态");
					} else {
						$('#window_state').text("封禁状态");
					}
					$('#window_address').text(address);
					$('#window_phone').text(tel);
					$('#window_email').text(email);
					//渲染模板
					var getTpl = document.getElementById('window_order_xx').innerHTML,
						view = document.getElementById('window_moban_view');
					laytpl(getTpl).render(data, function(html) {
						view.innerHTML = html;
					});
				}
			});

			//打开弹窗
			layer.open({
				type: 1,
				area: ['1000px', '600px'],
				title: '订单处理',
				content: $("#window_orderxx"),
				shade: 0,
				btn: ['确定'],
				btn1: function(index, layero) {
					layer.closeAll();
				},
				cancel: function(layero, index) {
					layer.closeAll();
				}
			});
		}
	})
});
